package com.kehao.newcoder;

public class NC48Solution {
    public int search (int[] nums, int target) {
        int minIndex = 0 ,maxIndex = nums.length-1;
        if(nums[0]==target)return 0;
        if(nums[nums.length-1]==target)return nums.length-1;
        while (minIndex<=maxIndex){
            int middleIndex = (minIndex+maxIndex)/2;
            if(nums[middleIndex]==target) return middleIndex;
            //左边有序
            if(nums[0]<=nums[middleIndex]){
                if(target>nums[0]&&target<nums[middleIndex]){
                    maxIndex = middleIndex -1;
                }else {
                    minIndex = middleIndex +1;
                }
            }else {
                //右边有序
                if(target<nums[nums.length-1]&&target>nums[middleIndex]){
                    minIndex = middleIndex +1;
                }else {
                    maxIndex = middleIndex -1;
                }
            }
        }
        return -1;
    }
}
